package com.ytc.information.service.Impl;

import com.ytc.company.mapper.CompanyMapper;
import com.ytc.company.model.Company;
import com.ytc.information.mapper.DeliverMapper;
import com.ytc.information.mapper.InformationMapper;
import com.ytc.information.mapper.RecordMapper;
import com.ytc.information.model.*;
import com.ytc.information.service.InformationService;
import com.ytc.mine.mapper.ResumeMapper;
import com.ytc.mine.model.Resume;
import com.ytc.position.mapper.PositionMapper;
import com.ytc.position.model.Position;
import com.ytc.user.mapper.UserMapper;
import com.ytc.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class InformationServiceImpl implements InformationService {

    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private DeliverMapper deliverMapper;
    @Autowired
    private ResumeMapper resumeMapper;

    @Override
    public Information getInformation(Integer positionId,Integer userId){

        Information information = new Information();

        User user = userMapper.selectByPrimaryKey(userId);//应聘者数据
        Position position = positionMapper.get(positionId);//职位信息
/*
        if (user.getUserRole() == 00) {//若是用户
            information.setName(position.getPositionName());//当前用户的对话方就是职位名
        } else {//若是公司
            information.setName(user.getUserName());//对话方就是用户
        }
*/
        information.setCompanyId(position.getCompanyId());//公司id
        information.setPositionId(positionId);//简历id
        information.setUserId(userId);//应聘者id
        information.setInformationList(informationMapper.getInformationList(positionId,userId));//消息类
        return information;
    }

    @Override
    public void addInformation(InformationList informationList){
        informationList.setTime(new Date());//设置时间
        informationMapper.addInformation(informationList);

        Record1 record1 = new Record1();
        record1.setCompanyId(informationList.getCompanyId());
        record1.setPositionId(informationList.getPositionId());
        record1.setUserId(informationList.getUserId());
        Integer record = recordMapper.getRecord(record1);
        if(record == 0){
            recordMapper.addRecord(record1);
        }
    }

    //招聘者，消息页面查询所有有关的消息
    @Override
    public List<Message> getInformationList(Integer userId){
        //根据账号id查询公司id
        Company company = companyMapper.getCompany(userId);
        return recordMapper.getCompanyId(company.getCompanyId(),null);

    }


    //招聘者，消息页面查询所有有关的消息
    @Override
    public List<Message> getInformationList1(Integer userId){
        //根据账号id查询公司id
        return recordMapper.getCompanyId(null,userId);

    }

    //投递
    @Override
    public void addDeliver(Deliver deliver){
        Resume resume = resumeMapper.getResume(deliver.getUserId());
        deliver.setResumeId(resume.getResumeId());
        deliverMapper.addDeliver(deliver);

    }

    @Override
    public Deliver getDeliver(Deliver deliver){
        return deliverMapper.getDeliver(deliver);
    }
}
